package com.nopay.dxdwphone.ui.ativity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.nopay.dxdwphone.base.BaseActivity;
import com.nopay.dxdwphone.bean.request.SaveCallTaskRequest;
import com.nopay.dxdwphone.bean.response.SaveCallTaskResponse;
import com.nopay.dxdwphone.dialog.DialogLoading;

import java.util.ArrayList;
import java.util.List;
import com.nopay.dxdwphone.CallGroupEnum;
import com.nopay.dxdwphone.Constants;
import com.nopay.dxdwphone.R;
import com.nopay.dxdwphone.databinding.ActivityImportByClipBoardBinding;

import com.nopay.dxdwphone.ui.adapter.ImportByClipBoardAdapter;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FileImportByClipBoardActivity extends BaseActivity<ActivityImportByClipBoardBinding> {

    private final List<SaveCallTaskRequest.Data> mTaskData = new ArrayList<>();
    private ImportByClipBoardAdapter adapter;

    @Override
    protected void init(Bundle savedInstanceState, Object... preData) {
        binding.rlRoot.setPadding(0, getStatusBarHeight(FileImportByClipBoardActivity.this), 0, 0);
        binding.tvCancel.setOnClickListener(v -> mSwipeBackHelper.backward());

        adapter = new ImportByClipBoardAdapter(FileImportByClipBoardActivity.this, mTaskData);
        binding.lvImportPreview.setAdapter(adapter);

        binding.etClipBoard.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mTaskData.clear();
                if(!"".equals(s.toString())) {
                    String content = s.toString().replace("\t", ",").replace("\n", ",").replace(" ", "");
                    String[] phoneList = content.split(",");
                    for (String phone : phoneList) {
                        if(RegexUtils.isMobileSimple(phone)) {
                            SaveCallTaskRequest.Data data = new SaveCallTaskRequest.Data();
                            data.setPhone(phone);
                            mTaskData.add(data);
                        }
                    }
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        binding.tvImport.setOnClickListener(v -> {
            // TODO 导入
            if(mTaskData.size() == 0) {
                ToastUtils.showShort("未识别到手机号码");
                return;
            }

            importCallTask();
        });
    }

    private DialogLoading mDialogLoading;

    public void importCallTask() {
        if(mDialogLoading == null) mDialogLoading = new DialogLoading(FileImportByClipBoardActivity.this);
        mDialogLoading.show("粘贴导入中...");

        SaveCallTaskRequest bean = new SaveCallTaskRequest();
        bean.setPhone(SPUtils.getInstance().getString(Constants.SP_PHONE, ""));
        bean.setImportGroup(CallGroupEnum.IMPORT_PASTE);
        bean.setPersonnelInfoSaveReqVOs(mTaskData);

        RequestBody body = getRequestBody(bean);
        api.doSaveCallTaskToGroup(body).enqueue(new Callback<SaveCallTaskResponse>() {
            @Override
            public void onResponse(Call<SaveCallTaskResponse> call, Response<SaveCallTaskResponse> response) {
                mDialogLoading.dismiss();
                if (response.body() != null) {
                    if (Constants.RESULT_OK.equals(response.body().getCode())) {
                        ToastUtils.showShort("导入成功");

                        mSwipeBackHelper.backward();
                    } else {
                        ToastUtils.showShort(response.body().getMsg());
                    }
                }
            }

            @Override
            public void onFailure(Call<SaveCallTaskResponse> call, Throwable t) {
                mDialogLoading.dismiss();
                ToastUtils.showShort(FileImportByClipBoardActivity.this.getResources().getString(R.string.call_api_error));
            }
        });
    }
}
